package 哈希;

import java.util.HashSet;
import java.util.Set;

/**
 * @author tmh
 * @date 2025/3/6 17:05
 * @description
 */
public class T128最长连续序列 {
    public int longestConsecutive(int[] nums) {
        int maxLength= 0;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        for (int num : nums) {
            if (!set.contains(num-1)){
                int y=num;
                while (set.contains(y+1)){
                    y++;
                }
                int len = y-num+1;
                maxLength=Math.max(maxLength,len);
            }
        }
        return maxLength;
    }
}
